var step = 1;
/**
 * 载入注册第一步界面
 * 定义回车动作
 */
$(document).ready(function () {
    $('#content').load('./register_1.html');
    $(this).keydown(function (e) {
        if (e.which == '13' && step == 1) {
            register_1();
        } else if (e.which == '13' && step == 2 && $.data(document.body, "user_type") == '1') {
            register_2_school();
        } else if (e.which == '13' && step == 2 && $.data(document.body, "user_type") == '2') {
            register_2_county();
        } else if (e.which == '13' && step == 2 && $.data(document.body, "user_type") == '3') {
            register_2_city();
        } else if (e.which == '13' && step == 2 && $.data(document.body, "user_type") == '4') {
            register_2_leader();
        } else if (e.which == '13' && step == 3) {
            register_3();
        } else {

        }
    })
})

/**
 *  “上一步”函数
 */
function backward() {
    if ($.data(document.body, "user_type") == '1' && step == 3) {
        window.location.href = './register_2_school.html';
        step--;
    } else if ($.data(document.body, "user_type") == '2' && step == 3) {
        window.location.href = './register_2_county.html';
        step--;
    } else if ($.data(document.body, "user_type") == '2' && step == 3) {
        window.location.href = './register_2_city.html';
        step--;
    } else if ($.data(document.body, "user_type") == '2' && step == 3) {
        window.location.href = './register_2_leader.html';
        step--;
    } else if (step == 2) {
        $('#content').load('./register_1.html');
        step--;
    } else {

    }
}

/**
 *  注册第一步：根据账户类型进入对应注册第二步界面
 *  记录账号类型和凭证码
 */
function register_1() {
    var user_type = $('#account_type').val();
    var code = $('#code').val();
    if (user_type != '1' && user_type != '2' && user_type != '3' && user_type != '4') {
        alert("请选择账户类型");
        return;
    }
    if (code == '') {
        alert("请填写注册凭证");
        return;
    }
    $.ajax({
        type: 'POST',
        url: '/management/register/1',
        data: {
            userType: user_type,
            code: code
        },
        dataType: 'json',
        success: function (res) {
            if (res.code == 200) {
                var to = "";
                switch (user_type) {
                    case '1':
                        to = "./register_2_school.html";
                        break;
                    case '2':
                        to = "./register_2_county.html";
                        break;
                    case '3':
                        to = "./register_2_city.html";
                        break;
                    case '4':
                        to = "./register_2_leader.html";
                        break;
                    default:
                        break;
                }
                // 保存用户类型
                $.data(document.body, "user_type", user_type);
                $.data(document.body, 'code', code);
                $('#content').load(to);
                step++;
            } else {
                alert(res.msg);
            }
        },
        error: function () {
            alert("服务器异常");
        }
    });
}

/**
 * 注册第二步（学校用户）
 */
function register_2_school() {
    var school_name = $('#school_name').val();
    var school_id = $('#school_id').val();
    var school_englishName = $('#school_englishname').val();
    var school_address = $('#school_address').val();
    var schoolUser_phone = $('#schoolUser_phone').val();

    if (school_name == '') {
        alert('请输入学校名称');
        return;
    } else if (school_name.length > 20) {
        alert('学校名称长度不能超过20');
        return;
    } else if (school_id == '') {
        alert('请输入学校编号');
        return;
    } else if (school_id.length > 5) {
        alert('学校编号长度不能超过5');
        return;
    } else if (school_englishName == '') {
        alert('请输入学校英文名称');
        return;
    } else if (school_englishName.length > 50) {
        alert('学校英文名称长度不能超过50');
        return;
    } else if (school_address == '') {
        alert('请输入学校地址');
        return;
    } else if (school_address.length > 50) {
        alert('学校地址长度不能超过50');
        return;
    } else if (schoolUser_phone == '') {
        alert('请输入联系电话');
        return;
    } else if (schoolUser_phone.length > 20) {
        alert('联系电话长度不能超过20');
        return;
    } else {
        $.ajax({
            type: 'POST',
            url: '/management/register/2/school',
            data: {
                school_id: school_id
            },
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    $.data(document.body, 'school_name', school_name);
                    $.data(document.body, 'school_id', school_id);
                    $.data(document.body, 'school_englishName', school_englishName);
                    $.data(document.body, 'school_address', school_address);
                    $.data(document.body, 'schoolUser_phone', schoolUser_phone);
                    // 加载第三界面
                    $('#content').load('./register_3.html');
                    step++;
                } else {
                    alert(res.msg);
                    return;
                }
            },
            error: function () {
                alert('服务器异常');
                return;
            }
        });
    }
}

/**
 *  注册第二步（区县用户）
 */
function register_2_county() {
    var countyEdbName = $('#countyEdbName').val();
    var adminCode = $('#adminCode').val();
    var countyAddress = $('#countyAddress').val();
    var countyUserPhone = $('#countyUserPhone').val();

    if (countyEdbName == '') {
        alert('请输入区县教育局名称');
        return;
    } else if (countyEdbName.length > 20) {
        alert('区县教育局名称长度不能超过20');
        return;
    } else if (adminCode == '') {
        alert('请输入部门行政编号');
        return;
    } else if (adminCode.length > 5) {
        alert('部门行政编号长度不能超过5');
        return;
    } else if (countyAddress == '') {
        alert('请输入办公地址');
        return;
    } else if (countyAddress.length > 50) {
        alert('办公地址长度不能超过50');
        return;
    } else if (countyUserPhone == '') {
        alert('请输入办公电话');
        return;
    } else if (countyUserPhone.length > 20) {
        alert('办公电话长度不能超过20');
        return;
    } else {
        $.ajax({
            type: 'POST',
            url: '/management/register/2/county',
            data: {
                adminCode: adminCode
            },
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    $.data(document.body, 'countyEdbName', countyEdbName);
                    $.data(document.body, 'adminCode', adminCode);
                    $.data(document.body, 'countyAddress', countyAddress);
                    $.data(document.body, 'countyUserPhone', countyUserPhone);
                    // 加载第三界面
                    $('#content').load('./register_3.html');
                    step++;
                } else {
                    alert(res.msg);
                    return;
                }
            },
            error: function () {
                alert('服务器异常');
                return;
            }
        });
    }
}

/**
 * 注册第二步（市局用户）
 */
function register_2_city() {
    var cityEdbName = $('#cityEdbName').val();
    var adminCode = $('#adminCode').val();
    var cityAddress = $('#cityAddress').val();
    var cityUserPhone = $('#cityUserPhone').val();

    if (cityEdbName == '') {
        alert('请输入市教育局名称');
        return;
    } else if (cityEdbName.length > 20) {
        alert('市教育局名称长度不能超过20');
        return;
    } else if (adminCode == '') {
        alert('请输入部门行政编号');
        return;
    } else if (adminCode.length > 5) {
        alert('部门行政编号长度不能超过5');
        return;
    } else if (cityAddress == '') {
        alert('请输入办公地址');
        return;
    } else if (cityAddress.length > 50) {
        alert('办公地址长度不能超过50');
        return;
    } else if (cityUserPhone == '') {
        alert('请输入办公电话');
        return;
    } else if (cityUserPhone.length > 20) {
        alert('办公电话长度不能超过20');
        return;
    } else {
        $.ajax({
            type: 'POST',
            url: '/management/register/2/city',
            data: {
                adminCode: adminCode
            },
            dataType: 'json',
            success: function (res) {
                if (res.code == 200) {
                    $.data(document.body, 'cityEdbName', cityEdbName);
                    $.data(document.body, 'adminCode', adminCode);
                    $.data(document.body, 'cityAddress', cityAddress);
                    $.data(document.body, 'cityUserPhone', cityUserPhone);
                    // 加载第三界面
                    $('#content').load('./register_3.html');
                    step++;
                } else {
                    alert(res.msg);
                    return;
                }
            },
            error: function () {
                alert('服务器异常');
                return;
            }
        });
    }
}

/**
 * 注册第二步（高级用户）
 */
function register_2_leader() {
    var leaderUserName = $('#leaderUserName').val();
    var leaderUserPhone = $('#leaderUserPhone').val();

    if (leaderUserName == '') {
        alert('请输入姓名');
        return;
    } else if (leaderUserName.length > 10) {
        alert('姓名长度不能超过10');
        return;
    } else if (leaderUserPhone == '') {
        alert('请输入办公电话');
        return;
    } else if (leaderUserPhone.length > 20) {
        alert('办公电话长度不能超过20');
        return;
    } else {
        $.data(document.body, 'leaderUserName', leaderUserName);
        $.data(document.body, 'leaderUserPhone', leaderUserPhone);
        // 加载第三界面
        $('#content').load('./register_3.html');
        step++;
    }
}

/**
 * 注册第三步：检验输入格式
 * 根据用户类型，访问后台注册方法
 */
function register_3() {
    var user_id = $('#user_id').val();
    var user_pwd = $('#user_pwd').val();
    var user_pwd_2 = $('#user_pwd_2').val();

    if (user_id == '') {
        alert('请输入登录用户名');
        return;
    } else if (user_id.length > 50) {
        alert('用户名长度不能超过50');
        return;
    } else if (user_pwd == '') {
        alert('请输入登录密码');
        return;
    } else if (user_pwd.length > 32) {
        alert('密码长度不能超过32');
        return;
    } else if (user_pwd_2 != user_pwd) {
        alert('密码不一致');
        return;
    } else {
        switch ($.data(document.body, 'user_type')) {
            case '1' :
                register_school(user_id, user_pwd);
                break;
            case '2' :
                register_county(user_id, user_pwd);
                break;
            case '3' :
                register_city(user_id, user_pwd);
                break;
            case '4' :
                register_leader(user_id, user_pwd);
                break;
            default :
                alert('注册状态出错，请返回第一步');
                return;
        }

    }
}

/**
 * 注册学校用户
 * @param schoolUser_id
 * @param schoolUser_pwd
 */
function register_school(schoolUser_id, schoolUser_pwd) {
    $.ajax({
        type: 'POST',
        url: '/management/register/3/school',
        data: {
            schoolUserId: schoolUser_id,
            certificate_code: $.data(document.body, 'code'),
            schoolUserPwd: schoolUser_pwd,
            schoolUserPhone: $.data(document.body, 'schoolUser_phone'),
            schoolName: $.data(document.body, 'school_name'),
            schoolEnglishname: $.data(document.body, 'school_englishName'),
            schoolAddress: $.data(document.body, 'school_address'),
            schoolId: $.data(document.body, 'school_id')
        },
        dataType: 'json',
        success: function (res) {
            if (res.code == 200) {

                window.location.href = './school_main.html';
            } else {
                alert(res.msg);
                return;
            }
        },
        error: function () {
            alert('服务器异常');
            return;
        }
    });
}

/**
 * 注册区县用户
 * @param countyUserId
 * @param countyUserPwd
 */
function register_county(countyUserId, countyUserPwd) {
    $.ajax({
        type: 'POST',
        url: '/management/register/3/county',
        data: {
            countyUserId: countyUserId,
            certificate_code: $.data(document.body, 'code'),
            countyUserPwd: countyUserPwd,
            countyEdbName: $.data(document.body, 'countyEdbName'),
            adminCode: $.data(document.body, 'adminCode'),
            countyAddress: $.data(document.body, 'countyAddress'),
            countyUserPhone: $.data(document.body, 'countyUserPhone')
        },
        dataType: 'json',
        success: function (res) {
            if (res.code == 200) {

                window.location.href = './county_main.html';
            } else {
                alert(res.msg);
                return;
            }
        },
        error: function () {
            alert('服务器异常');
            return;
        }
    });
}

/**
 * 注册市局用户
 * @param cityUserId
 * @param cityUserPwd
 */
function register_city(cityUserId, cityUserPwd) {
    $.ajax({
        type: 'POST',
        url: '/management/register/3/city',
        data: {
            cityUserId: cityUserId,
            certificate_code: $.data(document.body, 'code'),
            cityUserPwd: cityUserPwd,
            cityEdbName: $.data(document.body, 'cityEdbName'),
            adminCode: $.data(document.body, 'adminCode'),
            cityAddress: $.data(document.body, 'cityAddress'),
            cityUserPhone: $.data(document.body, 'cityUserPhone')
        },
        dataType: 'json',
        success: function (res) {
            if (res.code == 200) {

                window.location.href = './city_main.html';
            } else {
                alert(res.msg);
                return;
            }
        },
        error: function () {
            alert('服务器异常');
            return;
        }
    });
}

/**
 * 注册高级用户
 * @param leaderUserId
 * @param leaderUserPwd
 */
function register_leader(leaderUserId, leaderUserPwd) {
    $.ajax({
        type: 'POST',
        url: '/management/register/3/leader',
        data: {
            leaderUserId: leaderUserId,
            certificate_code: $.data(document.body, 'code'),
            leaderUserPwd: leaderUserPwd,
            leaderUserName: $.data(document.body, 'leaderUserName'),
            leaderUserPhone: $.data(document.body, 'leaderUserPhone')
        },
        dataType: 'json',
        success: function (res) {
            if (res.code == 200) {

                window.location.href = './leader_main.html';
            } else {
                alert(res.msg);
                return;
            }
        },
        error: function () {
            alert('服务器异常');
            return;
        }
    });
}